<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        table{
            border-collapse: collapse;
        }
    </style>
</head>
<body>
    <table border="1" cellspacing="5" cellpadding="10">
        <tr>
            <td><label><input type="checkbox" name="" id="checkAll">全选</label></td>
            <td><label><input type="checkbox" name="" id="recheck">反选</label></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" class="check"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" class="check"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" class="check"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" class="check"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="" class="check"></td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </table>
    <script>
        // 获取全选框
        var checkAll = document.getElementById('checkAll');
        // 获取选择框
        var check = document.getElementsByClassName('check');
        // 获取反选框,将当前选中的为假，没有选中的为真
        var recheck = document.getElementById('recheck');
        recheck.onclick = function () {
            for (var i = 0; i < check.length; i++) {
                if (check[i].checked == true) {
                    check[i].checked = false;
                } else {
                    check[i].checked = true;
                }
            }
        };
        checkAll.onclick = function () {
            for(var i = 0; i < check.length; i++) {
                check[i].checked = this.checked;
            }
        }
        for(var i = 0; i < check.length; i++) {
                check[i].onclick = function () {
                    var flag = true;
                    for (var i = 0; i < check.length; i++) {
                        if(!check[i].checked) {
                            flag = false;
                        }
                    }
                    checkAll.checked = flag;
                }
            }
    </script>
</body>
</html>